package com.hym.basedemo;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.hym.myapplication.R;


/**
 * Desc:属性动画demo研究
 * 体验结论:要想做出牛逼的属性动画，需要对动画的算法比较清楚才是关键
 * Created by Hym on 2016/9/18 14:36
 * Modified by
 * Modification：
 */
public class MainActivity extends Activity implements View.OnClickListener {
    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        //button1.setBackgroundColor(Color.parseColor("#FFFF8080"));
        button1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                //doTranslationAnimotion();
                //doColorAnimotion();
                //doAnimotionSet();
                doXmlAnimationSet();
        }
    }

    /**
     * 做位置变化的属性动画
     */
    private void doTranslationAnimotion(){
        ObjectAnimator.ofFloat(findViewById(R.id.button1), "translationY", -button1.getHeight()).start();
    }

    /**
     * 做颜色变化的属性动画
     */
    private void doColorAnimotion() {
        //通过valueAnimator设置背景的颜色变化
        ValueAnimator colorAnimator=ObjectAnimator.ofInt(button1,"backgroundColor",0xFFFF8080,0xFF8080FF);
        colorAnimator.setDuration(3000);
        //设置数值计算器
        colorAnimator.setEvaluator(new ArgbEvaluator());
        //无限循环
        colorAnimator.setRepeatCount(ValueAnimator.INFINITE);
        //设置重复播放的规则是逆向还是重播
        colorAnimator.setRepeatMode(ValueAnimator.REVERSE);
        colorAnimator.start();
    }

    /**
     * 执行一个动画集合，旋转，平移，缩放和透明度的变化
     */
    private void doAnimotionSet(){
        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.playTogether(ObjectAnimator.ofFloat(button1,"rotationX",0,360),
                ObjectAnimator.ofFloat(button1,"rotationY",0,180),
                ObjectAnimator.ofFloat(button1,"rotationX",0,-90),
                ObjectAnimator.ofFloat(button1,"translationX",0,90),
                ObjectAnimator.ofFloat(button1,"translationY",0,90),
                ObjectAnimator.ofFloat(button1,"scaleX",1,1.5f),
                ObjectAnimator.ofFloat(button1,"scaleY",1,0.5f),
                ObjectAnimator.ofFloat(button1,"alpha",1,0f,1));
        animatorSet.setDuration(5*1000).start();
    }

    /**
     * 通过xml做平移的属性动画
     */
    private void doXmlAnimationSet(){
        AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this,
                R.animator.property_animator1);
        set.setTarget(button1);
        set.start();
    }
}

